package com.xiaohei.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaohei.crm.common.result.Result;
import com.xiaohei.crm.dao.User;
import com.xiaohei.crm.dao.dto.UserLoginReqDTO;
import com.xiaohei.crm.dao.dto.UserLoginRespDTO;
import com.xiaohei.crm.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserController
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/6/19 15:06
 * Version 1.0
 */
@RestController
@RequiredArgsConstructor
@CrossOrigin
public class UserController {
    private final UserService userService;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public Result<User> getUserByUserName(@PathVariable String username){
        User user = userService.getByUsername(username);
        return Result.ok(user);
    }

    /**
     * 分页查询用户信息
     * @return
     */
    @GetMapping("/user/findByPage")
    public Result<IPage> findByPage(){
        return userService.findByPage();
    }

    /**
     * 添加用户
     * @param user   请求参数
     * @return
     */
    @PostMapping("/user")
    public Result save(@RequestBody User user){
        userService.saveUser(user);
        return Result.ok();
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/user")
    public Result update(@RequestBody User user){
        userService.updateByUsername(user);
        return Result.ok();
    }

    /**
     * 逻辑删除用户
     * @param username
     * @return
     */
    @DeleteMapping("user/{username}")
    public Result<String> deleteByUsername(@PathVariable("username") String username){
        userService.removeByUsername(username);
        return Result.ok();
    }

    /**
     * 用户登录方法
     */
    @PostMapping("/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParams){
        return Result.ok(userService.login(requestParams));
    }

    /**
     * 检查用户是否登录的接口
     * @param token  用户的token信息
     * @return
     */
    @GetMapping("/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Result.ok(userService.checkLogin(username,token));
    }

    /**
     * 退出登录
     * @return
     */
    @DeleteMapping("/user/logout")
    public Result<Void> logOut(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logOut(username,token);
        return Result.ok();
    }

    /**
     * 用户登录方法
     */
    @PostMapping("/user/login/jwt")
    public Result<UserLoginRespDTO> login(HttpServletRequest request, HttpServletResponse response, @RequestBody UserLoginReqDTO requestParams){
        return Result.ok(userService.loginWithJwt(request,response,requestParams));
    }

}
